/*
 * Copyright 2002-2007 the original author or authors.
 *
 * Licensed under the Apache license, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package org.internna.iwebjtracker.services;

import javax.annotation.security.RolesAllowed;
import org.internna.iwebmvc.logging.Log4JRuntimeConfigurator;
import org.internna.iwebmvc.logging.Log4JRuntimeConfigurator.LOG_LEVEL;
import org.internna.iwebmvc.utils.Assert;
import org.springframework.beans.factory.annotation.Required;
import static org.internna.iwebmvc.utils.StringUtils.hasText;

/**
 * Allows secured remote administration operations.
 *
 * @author Jose Noheda
 * @since 1.0
 */
public final class AJAXAdministrator implements Administrator {

    private Log4JRuntimeConfigurator log4j;

    /**
     * Setter for Spring dependency injection.
     *
     * @param log4j
     */
    @Required public void setLog4j(Log4JRuntimeConfigurator log4j) {
        Assert.notNull(log4j);
        this.log4j = log4j;
    }

    /**
     * Allows administrators to change the log4j configuration on real time. If
     * the level specified is not valid it defaults to DEBUG.
     *
     * @param logger any
     * @param level any
     */
    @RolesAllowed("ROLE_ADMINISTRATOR")
    @Override public void changeLoggerLevel(String logger, String levelIdentifier) {
        if (hasText(logger) && hasText(levelIdentifier)) {
            LOG_LEVEL level = LOG_LEVEL.from(levelIdentifier);
            log4j.changeLoggerLevel(logger, level, true);
        }
    }

}
